//
//  LBTabBarController.m
//  XianYu
//
//  Created by li  bo on 16/5/28.
//  Copyright © 2016年 li  bo. All rights reserved.
//

#import "LBTabBarController.h"
#import "LBTabBar.h"
#import "UIImage+Image.h"
#import "THJMainController.h"
#import "THJDataVC.h"
#import "THJNewsVC.h"
#import "THJUserVC.h"
#import "ImagePickerViewController.h"
#import "THJPurchaseVC.h"
#import "THJFabuSupplyVC.h"
#import "THJLoginVC.h"
#import "THJLoginVC.h"
#import "THJPurchaseVC.h"
#import "ZBNetworking.h"
@interface LBTabBarController ()<LBTabBarDelegate,ImagePickerViewControllerDelegate,UITabBarDelegate,UITabBarControllerDelegate>
{
    //进入发布页面的类型 （现货，团购等等。。。。）
    NSString * jumpType;
    UINavigationController *nav;
    NSInteger fabuBtnClickFlag;
}
@end

@implementation LBTabBarController
#pragma mark ----LBTabBarController单例-----
static LBTabBarController *_sharedTabBarC = nil;

+ (instancetype)sharedTabBarC {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedTabBarC = [[super allocWithZone:NULL] init];
    });
    
    return _sharedTabBarC;
}

+ (id)allocWithZone:(struct _NSZone *)zone {
    return [LBTabBarController sharedTabBarC];
}

- (id)copyWithZone:(struct _NSZone *)zone {
    return [LBTabBarController sharedTabBarC];
}

#pragma mark - 第一次使用当前类的时候对设置UITabBarItem的主题
+ (void)initialize
{
    UITabBarItem *tabBarItem;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
       tabBarItem = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];
    }else{
        tabBarItem = [UITabBarItem appearanceWhenContainedIn:[self class], nil];
    }
    NSMutableDictionary *dictNormal = [NSMutableDictionary dictionary];
    dictNormal[NSForegroundColorAttributeName] = COLOR(139, 140, 141, 1);
    dictNormal[NSFontAttributeName] = [UIFont systemFontOfSize:11];

    NSMutableDictionary *dictSelected = [NSMutableDictionary dictionary];
    dictSelected[NSForegroundColorAttributeName] = [UIColor whiteColor];
    dictSelected[NSFontAttributeName] = [UIFont systemFontOfSize:11];

    [tabBarItem setTitleTextAttributes:dictNormal forState:UIControlStateNormal];
    [tabBarItem setTitleTextAttributes:dictSelected forState:UIControlStateSelected];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUpAllChildVc];
    fabuBtnClickFlag = 1;
    //创建自己的tabbar，然后用kvc将自己的tabbar和系统的tabBar替换下
    LBTabBar *tabbar = [[LBTabBar alloc] init];
    tabbar.myDelegate = self;
    tabbar.delegate = self;
    
    UITabBar *tabBar = [UITabBar appearance];
    [tabBar setBarTintColor:COLOR(55, 56, 57, 1)];
    tabBar.translucent = NO;
    //kvc实质是修改了系统的_tabBar
    [self setValue:tabbar forKeyPath:@"tabBar"];
    self.delegate = self;
    UINavigationBar *navBar = [UINavigationBar appearance];
//    navBar.barTintColor = COLOR(83, 84, 85, 1);
    NSDictionary *dict = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    [navBar setTitleTextAttributes:dict];
    
    
    
}
-(NSString *)base64StringFromText:(NSString *)text {
    NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String = [data base64EncodedStringWithOptions:0];
    return base64String;
}
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
//    [[LBTabBarController sharedTabBarC].fabuView removeFromSuperview];
    if([viewController.tabBarItem.title isEqual:@"我的"]){
        NSLog(@"点击了我的");
        NSString * token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
        if(token.length == 0){
            THJLoginVC * vc = [[THJLoginVC alloc] init];
            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
            [self presentViewController:nav animated:YES completion:nil];
            return NO;
        }else{
            NSLog(@"已登录");
            return YES;
        }
    }else{
        return YES;
    }
}
-(void)alertLogin{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录" preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self actionSheetIndex1Function];
    }]];
    
    [self presentViewController:alertController animated:YES completion:nil];
}
-(void)actionSheetIndex1Function{
    NSLog(@"好的");
    [self presentViewController:[THJLoginVC new] animated:YES completion:nil];
}
-(void)actionSheetIndex2Function{
    NSLog(@"取消");
}
#pragma mark - ------------------------------------------------------------------
#pragma mark - 初始化tabBar上除了中间按钮之外所有的按钮

- (void)setUpAllChildVc{
    
    THJMainController *HomeVC = [[THJMainController alloc] init];
    [self setUpOneChildVcWithVc:HomeVC Image:@"homeBtn" selectedImage:@"homeBtn2" title:@"首页"];
    THJDataVC *FishVC = [[THJDataVC alloc] init];
    [self setUpOneChildVcWithVc:FishVC Image:@"storeBtn" selectedImage:@"storeBtn2" title:@"商城"];
    THJNewsVC *MessageVC = [[THJNewsVC alloc] init];
    [self setUpOneChildVcWithVc:MessageVC Image:@"newsBtn" selectedImage:@"newsBtn2" title:@"资讯"];
    THJUserVC *MineVC = [[THJUserVC alloc] init];
    [self setUpOneChildVcWithVc:MineVC Image:@"mineBtn" selectedImage:@"minBtn2" title:@"我的"];
}

#pragma mark - 初始化设置tabBar上面单个按钮的方法
/**
 *  @author li bo, 16/05/10
 *
 *  设置单个tabBarButton
 *
 *  @param Vc            每一个按钮对应的控制器
 *  @param image         每一个按钮对应的普通状态下图片
 *  @param selectedImage 每一个按钮对应的选中状态下的图片
 *  @param title         每一个按钮对应的标题
 */
- (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title
{
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:Vc];
    
    UIImage *myImage = [UIImage imageNamed:image];
    myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    //tabBarItem，是系统提供模型，专门负责tabbar上按钮的文字以及图片展示
    Vc.tabBarItem.image = myImage;

    UIImage *mySelectedImage = [UIImage imageNamed:selectedImage];
    mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    Vc.tabBarItem.selectedImage = mySelectedImage;

    Vc.tabBarItem.title = title;

    Vc.navigationItem.title = title;

    [self addChildViewController:nav];
}
#pragma mark - ------------------------------------------------------------------
#pragma mark - LBTabBarDelegate
//点击中间发布的代理方法
- (void)tabBarPlusBtnClick:(LBTabBar *)tabBar
{
    NSLog(@"点击发布");
    NSString * token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    if(token.length==0){
        [self alertLogin];
    }else{
        if(fabuBtnClickFlag){
            UIWindow * window = [UIApplication sharedApplication].keyWindow;
            [window addSubview:self.fabuView];
            fabuBtnClickFlag = 0;
        }else{
            [self.fabuView removeFromSuperview];
            fabuBtnClickFlag = 1;
        }
    }
}
-(UIView*)fabuView{
    if(!_fabuView){
        _fabuView = [[UIView alloc]init];
        _fabuView.frame = CGRectMake((ScreenWidth - 80)/2, ScreenHeight - 160, 80, 100);
        _fabuView.backgroundColor = SingleColor(clearColor);
        _fabuView.layer.cornerRadius = 5;
        NSArray * titleArr = @[@"供应",@"求购",@"供应"];
        UIImageView * imgV = [[UIImageView alloc] init];
        imgV.frame = CGRectMake(25, 71, 30, 30);
        imgV.image = kGetImage(@"fabujt");
        [_fabuView addSubview:imgV];
        
        UIView * lineview = [[UIView alloc] init];
        lineview.frame = CGRectMake(0,40, 80, 1);
        lineview.backgroundColor = SingleColor(lightGrayColor);
        [_fabuView addSubview:lineview];
        
        for (NSInteger i = 0; i<2; i++) {
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
            btn.backgroundColor = COLOR(57, 61, 63, 1);
            btn.titleLabel.font = [UIFont systemFontOfSize:16];
            [btn setTitleColor:SingleColor(lightGrayColor) forState:UIControlStateNormal];
            [btn setTitle:titleArr[i] forState:UIControlStateNormal];
            btn.frame = CGRectMake(0, i*(40+1), 80, 40);
            [btn addTarget:self action:@selector(fabu:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = i+100;
            [_fabuView addSubview:btn];
        }
    }
    return _fabuView;
}
-(void)fabu:(UIButton*)btn{
    fabuBtnClickFlag = 1;
    [self.fabuView removeFromSuperview];
    if(btn.tag == 100){
        THJPurchaseVC * vc = [THJPurchaseVC new];
        vc.PTitle = @"发布供应";
        UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc];
        [self presentViewController:nav animated:YES completion:^{
            
        }];
    }else{
        THJFabuSupplyVC * vc = [THJFabuSupplyVC new];
        vc.STitle = @"发布求购";
        UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc];
        [self presentViewController:nav animated:YES completion:^{
        }];
    }
}
@end
